package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.sky.constant.MessageConstant.DISH_ON_SALE;

/**
 * @author iTaotao
 * @version 1.0.0
 * @ClassName SetMealServiceImpl.java
 * @Description 套餐实现类
 * @createTime 2023年07月28日 17:40:00
 */
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult  selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO , setmeal);
        setmeal.setCategoryId(setmealPageQueryDTO.getCategoryId());
        log.info("参数:{}",setmeal);
        Page<Setmeal> page = setMealMapper.selectPage(setmeal);
        return new PageResult(page.getTotal(), page.getResult());


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> setmealDish.setDishId(setmealDish.getId()));
            setmealDishMapper.insert(setmealDishes);
        }

    }

    @Override
    public void updateStatusById(Long id, Integer status) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setMealMapper.updateStatusById(setmeal);
    }

    @Override
    public void removeSetMealById(List<Long> ids) {
        //        在售的套餐不能删除
        Integer count = setMealMapper.selectMealByStatus(ids , StatusConstant.ENABLE);
        if(count > 0){
            throw new DeletionNotAllowedException(DISH_ON_SALE);
        }

        setMealMapper.deleteSetMealById(ids);
        setmealDishMapper.deletesetmealDishBySetMealId(ids);
    }

    @Override
    public SetmealVO selectSetMealById(Long id) {

        Setmeal setmeal = setMealMapper.selectSetMealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.seleteSetMealByid(id);
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealVO.setSetmealDishes(setmealDishes);
        }
        return setmealVO;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO , setmeal);
        setMealMapper.updateSetMeal(setmeal);

        ArrayList<Long> arrayList = new ArrayList<>();
        arrayList.add(setmealDTO.getId());
        setmealDishMapper.deletesetmealDishBySetMealId(arrayList);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> setmealDish.setDishId(setmealDish.getId()));
            setmealDishMapper.insert(setmealDishes);
        }

    }
}
